package com.ghc.zookeepermysql.controller;

import com.alibaba.druid.pool.DruidDataSource;
import com.ghc.zookeepermysql.ZookeeperMysqlApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@RestController
@RequestMapping("/")
public class TestController {

    @RequestMapping("/test")
    public String doSql() throws SQLException {
        DruidDataSource druidDataSource = ZookeeperMysqlApplication.druidDataSource;
        System.out.println(druidDataSource.getClass());
        //获得数据库连接
        Connection connection = druidDataSource.getConnection();
        PreparedStatement pst = connection.prepareStatement("select * from account");
        ResultSet rs = pst.executeQuery();
        StringBuilder result = new StringBuilder();
        while (rs.next()) {
            result.append("id : ").append(rs.getString(1))
                    .append(" , username : ").append(rs.getString(2))
                    .append(" , money : ").append(rs.getString(3));
        }
        //关闭
        connection.close();
        return result.toString();
    }
}
